package com.livefilms.main;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.SocketTimeoutException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.jsoup.HttpStatusException;
import org.jsoup.Jsoup;
import org.jsoup.UnsupportedMimeTypeException;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class HttpVideo {

	public static void main(String[] args) {
	    String URL = "http://www.cb01.org/lultimo-dei-mohicani-1992/";
		HttpVideo video = new HttpVideo();
		List<Urls> urls = video.getListVideo(URL,1);
		
		for (Urls url : urls) {
			System.out.print("HOST: "+url.getHost());
			System.out.println(" URL: "+url.getUrl());
		}
	}
	
	
	public List<Urls> getListVideo(String  url, long id){
	
		String USER_AGENT ="Mozilla/5.0 (Windows NT 6.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 Safari/537.36"; 
		
		Document doc = null;
		List<Urls> listHost = new ArrayList<Urls>();
		try {
			doc = Jsoup.connect(url).userAgent(USER_AGENT).timeout(20000).get();
		    //DATA = doc.getElementById("blogmeta").text();
			
			Elements divs = doc.select("div[data-src]");
			for (Element link : divs) {
				String host = isVideo(link.attr("data-src"));
				if(host != null){
					url = link.attr("data-src");
					listHost.add(new Urls(host,resizeVideo(url), id,"VIDEO"));	
				}
			}
			
			Elements frames = doc.select("iframe[src]");
			for (Element link : frames) {
				
				String host = isVideo(link.attr("src"));
				if(host != null){
					url = link.attr("src");
					listHost.add(new Urls(host,resizeVideo(url), id,"VIDEO"));	
				}
			}
		}
		catch (IOException e) {
			System.out.println("Error:"+e);
		}
		return listHost;	
	}
	
	private Map<String, String> setHost(){
		Map<String, String> param = new HashMap<String, String>();
		param.put("cineblog01.pw".toUpperCase(),"CNE");
		param.put("videomeh.com".toUpperCase(),"VDO");
		param.put("moevideo.net".toUpperCase(),"MOE");
		param.put("vk.com".toUpperCase(),"VK");
		param.put("vidushare.com".toUpperCase(),"VSH");
		param.put("video.tt".toUpperCase(),"VTT");
		param.put("akstream.net".toUpperCase(),"AKS");
		param.put("api.video.mail".toUpperCase(),"API");
		param.put("speedvideo.net".toUpperCase(),"SPD");
		param.put("videomega.tv".toUpperCase(),"VDO");
		param.put("youtube.com".toUpperCase(),"TLR");
		
		return param;
	}
	
	
	
	private String isVideo(String url_video){
		Map<String, String> hsNameHost = this.setHost();
		Iterator<String> iter =  hsNameHost.keySet().iterator();		
		while(iter.hasNext()) {
				 String key = iter.next();
			     String value = hsNameHost.get(key);
			     if((url_video.toUpperCase()).contains(key)){
			    	 return value;
			     }
		}
		return null;
	}
	
	
	private String resizeVideo(String url){
		int width  = 410;
		int height = 270;
		url = url.replace("width=530", "width="+width).
				  replace("height=330", "height="+height).
				  replace("width=\\\"607\\\"", "width=\\\""+width+"\\\"").
				  replace("height=\\\"360\\\"", "height=\\\""+height+"\\\"").
				  replace("530x330", width+"x"+height);
 		return url;
	}
}
